from PyQt5.QtGui import QMouseEvent, QContextMenuEvent
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QMenu, QAction, QDialog, QMessageBox, QApplication

from Tools.Transform.Controller.TransformEvent import TransformEvent
import GUI.Window.Editor as WindowEditor
import Tools.Transform as Transform


class ControlledPanel(QWidget):
    TrackMouse = False  # 允许鼠标移动
    TrackMouseHandle = 0  # 允许鼠标移动-引用计数
    # MouseMoveSignal = pyqtSignal(QMouseEvent)  # 鼠标移动信号
    PreviousMousePos = None  # 上一次鼠标位置
    Context: QMenu = None  # 右键菜单
    FocusFlag = False
    isEditing = False  # 正在编辑， 一般是进行变换时
    currentController = None   #当前控制器

    # 变换类型
    Transform_Scaler = 1
    Transform_Rotater = 2

    # 部件
    imageWindow = None

    def __init__(self, imageWindow):
        """
        :type parent: QWidget
        """
        parent = imageWindow.imagePanel
        super().__init__(parent)
        # self.setAttribute(Qt.WA_StyledBackground, True)
        self.imageWindow = imageWindow
        self.initUI()
        self.hide()

    def initUI(self):
        # self.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        self.initContext()

    def initContext(self):
        # Content
        menu = QMenu(self)
        self.Context = menu

        Act = QAction("缩放", menu)
        menu.addAction(Act)
        Act.triggered.connect(self.__scaleAction)
        menu.scaleAct = Act

        Act = QAction("旋转", menu)
        menu.addAction(Act)
        Act.triggered.connect(self.__rotateAction)
        menu.rotateAct = Act

    def __scaleAction(self):
        if self.isEditing:
            controller = self.currentController
            if controller.Name != 'Scaler':
                # 保存当前控制器变换
                controller.confirm()
                # 新建控制器
                self.addTransform(self.Transform_Scaler)

    def __rotateAction(self):
        if self.isEditing:
            controller = self.currentController
            if controller.Name != 'Rotater':
                # 保存当前控制器变换
                controller.confirm()
                # 新建控制器
                self.addTransform(self.Transform_Rotater)

    def addTransform(self, transfromType=1):
        """
        给当前图层新建几何变换
        :param transfromType:
        :return:
        """
        if self.isEditing:
            self.aboutSave()
            if self.isEditing:
                return
        layer = self.imageWindow.currentLayer
        if layer:
            if transfromType == self.Transform_Scaler:
                controller = Transform.Scaler(self, layer)
                editorWidget = WindowEditor.newEditor()
                controller.setEditorWidget(editorWidget)
                self.addController(controller)
            elif transfromType == self.Transform_Rotater:
                controller = Transform.Rotater(self, layer)
                editorWidget = WindowEditor.newEditor()
                controller.setEditorWidget(editorWidget)
                self.addController(controller)

    def addController(self, controller):
        """
        添加指示器
        :param controller:
        :return:
        """
        self.currentController = controller
        self.isEditing = True
        self.TrackMouse = True
        self.show()
        # self.setFocusProxy(controller.editorWidget)

    def removeController(self, controller):
        """
        删除指示器
        :param controller:
        :return:
        """
        if controller == self.currentController:
            self.currentController = None
            self.isEditing = False
            self.TrackMouse = False
            self.hide()
            print('面板移除控制器')

    def mouseMoveEvent(self, event: QMouseEvent):
        if self.TrackMouse:
            controller = self.currentController
            if controller.Active:
                onTransformEvent = getattr(controller, 'onTransformEvent', None)
                if event:
                    transformEvent = TransformEvent()
                    transformEvent.PreviousMousePos = self.PreviousMousePos
                    transformEvent.CurrentMousePos = event.pos()
                    self.PreviousMousePos = event.pos()
                    onTransformEvent(transformEvent)

    def aboutSave(self):
        """
        先用户询问是否保存当前变换
        :return:
        """
        if self.isEditing:
            print('弹出窗口')
            button = QMessageBox.warning(self, "Warning", "是否应用当前变换",QMessageBox.Ok, QMessageBox.Cancel)
            if button == QMessageBox.Ok:
                self.currentController.confirm()
            else:
                self.currentController.cancel()
            # self.setEnabled(True)
            print('弹出窗体确认')

    def contextMenuEvent(self, event: QContextMenuEvent):
        # 将事件转发到view
        # self.setAttribute(Qt.WA_TransparentForMouseEvents, True)
        '''
        view = self.imageWindow.view
        newEvent = QContextMenuEvent(event.reason(), event.pos(), event.globalPos(), event.modifiers())
        QApplication.postEvent(view, newEvent)
        event.accept()
        # self.setAttribute(Qt.WA_TransparentForMouseEvents, False)
        '''
        self.Context.popup(self.mapToGlobal(event.pos()))
